package service

import (
	"cabinet/serializer"
	"cabinet/util"

	"github.com/gin-gonic/gin"
)

// UserTokenRefreshService 用户刷新token的服务
type UserTokenRefreshService struct {
}

type tokenRes struct {
	Token       string `json:"token,omitempty"`
	TokenExpire int64  `json:"token_expire,omitempty"`
}

// Refresh 刷新token
func (service *UserTokenRefreshService) Refresh(c *gin.Context) serializer.Response {
	uid, err := util.ExtractTokenID(c)
	if err != nil {
		return serializer.DBErr("redis err", err)
	}
	data := &tokenRes{}
	token, expire, err := util.GenerateToken(uid)
	if err != nil {
		return serializer.DBErr("redis err", err)
	}
	data.Token = token
	data.TokenExpire = expire
	return serializer.Success(data)
}
